package com.tduck.cloud.api.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tduck.cloud.account.entity.OrgEntity;
import com.tduck.cloud.account.entity.dto.OrgEntityDTO;
import com.tduck.cloud.account.entity.enums.TreeNodeWithSelfEnum;
import com.tduck.cloud.account.service.OrgService;
import com.tduck.cloud.common.util.QueryWrapperUtils;
import com.tduck.cloud.common.util.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/org")
@RestController
public class OrgController {
    private final OrgService orgService;

    public OrgController(OrgService orgService) {
        this.orgService = orgService;
    }

    @GetMapping("/page")
    public Result list(Page page, OrgEntity orgEntity) {
        Result result = new Result<>();
        QueryWrapper<OrgEntity> simpleQuery = QueryWrapperUtils.toSimpleQuery(orgEntity);
        simpleQuery.orderByDesc("create_time");
        return Result.success(orgService.page(page, simpleQuery));
    }

    @GetMapping(value = "/{id}")
    public Result<OrgEntity> getInfo(@PathVariable("id") String id) {
        return Result.success(orgService.getById(id));
    }

    @GetMapping("/tree")
    public Result<List<OrgEntityDTO>> getTree(Long id, TreeNodeWithSelfEnum withSelf) {
        return Result.success(orgService.queryAllChildNode(id, withSelf));
    }


}
